/**
 * @file App_OtaUDSUpgradeMain.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-08-05
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "stdint.h"
#include "stdlib.h"
#include "string.h"
#include "string.h"

#include "App_OtaUDSUpgradeMain.h"
#include "SYS_Disk.h"
#include "OtaData.h"
#include "system_init.h"
#include "OtaDataJson.h"
#include "OtaArrayQueueMng.h"
#include "SYS_OTAUpgrade.h"
#include "CheckFile.h"
#include "Md5.h"
#include "VehicleOta.h"
#include "SYS_Application.h"
#include "Diag_UDSClient.h"
#include "TigerTallyLock.h"

#ifdef PARTFROM_AC7
#include "Diag_OtaUDSConfig.h"
#endif
#ifdef PARTFROM_AC8
#include "Diag_OtaUDSConfigAC08.h"
#endif


extern t_OtaStateManage OtaStateManage;

static const uint32_t crc32_table[] =
{
    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
    0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
    0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
    0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
    0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
    0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
    0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
    0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
    0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
    0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
    0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
    0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
    0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
    0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
    0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
    0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
    0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
    0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
    0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
    0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
    0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
    0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
    0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
    0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
    0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
    0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
    0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
    0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
    0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
    0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
    0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
    0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
    0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
    0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
    0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
    0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
    0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
    0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
    0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
    0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
    0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
    0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
    0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
    0x2d02ef8d
};

Diag_UDSClientType UDSClient = {
    .ReqState = Diag_REQ_IDEL,
    .recvBuf = {0},
    .RecvDataType.RecvCallBack = (Diag_ClientCallback)NULL,
    .recvSize = 0,
    .requestTimeMs = 0,
    .maxTimeOutMs = 0,
    .sendBuf = {0},
    .sendSize = 0,
    .TpHandle = NULL,
    .SendDataType.Sequence = 0,
};




Diag_ErrType Ota_UDSInSession(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,Diag_UDSSessionType mode){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = &Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_DIAGNOSTIC_SESSION_CONTROL;
    client->responseId = responseId;
    return Diag_0X10_SendDiagSessCtrl(client,mode);
}

Diag_ErrType Ota_UDSSendRoutineCtrl(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,Diag_RoutineControlType type,
                            uint16_t routineIdentifier, const uint8_t *data, uint16_t size){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_ROUTINE_CONTROL;
    client->responseId = responseId;
    return Diag_0X31_SendRoutineCtrl(client,type,routineIdentifier,data,size);
}

Diag_ErrType Ota_UDSDTCCtrl(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,Diag_DTCModeType mode){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_CONTROL_DTC_SETTING;
    client->responseId = responseId;
    return Diag_0X85_DTCControl(client,mode);
}

Diag_ErrType Ota_UDSSendCommCtrl(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,Diag_UDSCommunicationControlType ctrl,Diag_UDSCommunicationType comm){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_COMMUNICATION_CONTROL;
    client->responseId = responseId;
    return Diag_0X28_SendCommCtrl(client, ctrl, comm);
}

Diag_ErrType Ota_UDSSendSecurityAccess(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId, uint8_t level, uint8_t *data, uint16_t size){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_SECURITY_ACCESS;
    client->responseId = responseId;
    return Diag_0X27_SendSecurityAccess(client,level,data,size);
}

Diag_ErrType Ota_UDSSendRequestDownload(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,uint8_t dataFormatIdentifier,
                                uint8_t addressAndLengthFormatIdentifier, uint32_t memoryAddress,
                                uint32_t memorySize){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_REQUEST_DOWNLOAD;
    client->responseId = responseId;
    return Diag_0X34_SendRequestDownload(client,dataFormatIdentifier,addressAndLengthFormatIdentifier,memoryAddress,memorySize);
}

Diag_ErrType Ota_UDSSendTransferData(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,uint8_t blockSequenceCounter,
                             const uint16_t blockLength, const uint8_t *data, uint16_t size){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_TRANSFER_DATA;
    client->responseId = responseId;
    return Diag_0X36_SendTransferData(client,blockSequenceCounter,blockLength,data,size);
}



Diag_ErrType Ota_UDSSendRequestTransferExit(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_REQUEST_TRANSFER_EXIT;
    client->responseId = responseId;
    return Diag_0X37_SendRequestTransferExit(client);
}

Diag_ErrType Ota_UDSSendEcuReset(Diag_UDSClientType *client,uint32_t phyId,uint32_t responseId,UDSECUReset_t type){
    Diag_ClearRequestContext(client);
    client->RecvDataType.RecvCallBack = Diag_RequestCommCallBack;
    client->ecuId = phyId;
    client->sid = Diag_SID_ECU_RESET;
    client->responseId = responseId;
    return Diag_0X11_SendEcuReset(client,type);
}


Ota_UDSErrType OTA_PrepareSendFrimData(tFileHead *fileHead,OtaFileType fileType,\
            uint16_t needReadBinLen,uint8_t *dstBuf,uint16_t dstBufLen,uint16_t *gotBinSize){

    static uint8_t remainBuf[128] = {0};  //用于保存上次读取数据内容保留
    static uint8_t lastRemainSize = 0; //用于保存上次读取剩余的数据长度保留
    static uint32_t lastBinAddr = 0;
    uint8_t readBuff[128] = {0};
    // uint16_t getBinSize = 0;
    uint16_t readedLen = 0;
    S19ErrCode ret;
    uint16_t i = 0;
    // uint16_t j = 0;
    str_S19LineData S19LineData;
    str_HEXLineData HEXLineData;
    // static uint32_t hexBaseAddr = 0;
    // static uint32_t hexLastAddr = 0;

    *gotBinSize = 0;
    memset(dstBuf,0x00,dstBufLen);
    if(dstBufLen < needReadBinLen){
        return Ota_UDS_GET_LEN_OUT_OF_RANGE;
    }
    if(lastRemainSize != 0){
        memmove(dstBuf,remainBuf,lastRemainSize);
        *gotBinSize += lastRemainSize;
        lastRemainSize = 0;
        //fprintf(USART2_STREAM,"-remain size:%d-%02x,%02x\r\n",lastRemainSize,remainBuf[0],remainBuf[1]);
    }
    switch (fileType)
    {
    case OTA_FILE_BIN:
        //直接从文件中获取并返回
        if(needReadBinLen<=2048){
            if(needReadBinLen + fileHead->readPtr >= fileHead->writePtr){
                //文件末尾长度检测，由于固定长度读取不会返回大小
                needReadBinLen = needReadBinLen + fileHead->readPtr - fileHead->writePtr;
                //fprintf(USART2_STREAM,"-get file end readPtr:%d,writePtr:%d",fileHead->readPtr,fileHead->writePtr);
            }
            readedLen = ReadFromFile(fileHead,dstBuf,needReadBinLen);
            if(readedLen == 0 || readedLen == 0xffff){
                return Ota_UDS_OK;
            }else{
                *gotBinSize = readedLen;
            }
        }else{
             return Ota_UDS_GET_LEN_OUT_OF_RANGE; //大小超出范围
        }
        break;
    case OTA_FILE_AFF:
        //fprintf(USART2_STREAM,"-uds this is .aff file");
        if(needReadBinLen<=2048){
            if(needReadBinLen + fileHead->readPtr > fileHead->writePtr){
                //文件末尾长度检测，由于固定长度读取不会返回大小
                needReadBinLen = needReadBinLen + fileHead->readPtr - fileHead->writePtr;
                //fprintf(USART2_STREAM,"-get file end readPtr:%d,writePtr:%d",fileHead->readPtr,fileHead->writePtr);
            }
            //fprintf(USART2_STREAM,"-needReadBinLen:%d\r\n",needReadBinLen);
            readedLen = ReadFromFile(fileHead,dstBuf,needReadBinLen);
            //fprintf(USART2_STREAM,"-get flie len:%d\r\n",readedLen);
            if(readedLen == 0 || readedLen == 0xffff){
                return Ota_UDS_OK;
            }else{
                *gotBinSize = readedLen;
            }
        }else{
             return Ota_UDS_GET_LEN_OUT_OF_RANGE; //大小超出范围
        }    
        break;
    case OTA_FILE_S19:
        for(;;){
            memset(readBuff,0x00,sizeof(readBuff));
            readedLen = ReadFromTextOneLine(fileHead,readBuff,sizeof(readBuff));
            //fprintf(USART2_STREAM,"-get flie len:%d\r\n",readedLen);
            if(readedLen == 0 || readedLen == 0xffff){
                return Ota_UDS_OK;
            }
            ret = validateS19String((const char*)readBuff,readedLen,&S19LineData);
            if(S19_NO_ERROR == ret){
                if(S19_TYPE_S1 == S19LineData.Type || S19_TYPE_S2 == S19LineData.Type || S19_TYPE_S3 == S19LineData.Type){
                    for ( i = 0; i < S19LineData.dataLen; i++)
                    {
                        dstBuf[(*gotBinSize)] = S19LineData.data[i];
                        (*gotBinSize)++;
                        if(*gotBinSize == needReadBinLen){
                            //传输完成
                            memset(remainBuf,0x00,sizeof(remainBuf));
                            lastRemainSize = S19LineData.dataLen - (i+1);
                            if(lastRemainSize) memmove(remainBuf,&S19LineData.data[i+1],lastRemainSize);
                            //fprintf(USART2_STREAM,"-remain:%d,S-dataLen:%d,i:%d\r\n",lastRemainSize,S19LineData.dataLen,i);
                            //fprintf(USART2_STREAM,"-[%x][%x][%x][%x][%x][%x]\r\n",remainBuf[0],remainBuf[1],remainBuf[2],remainBuf[3],remainBuf[4],remainBuf[5],remainBuf[6]);
                            return Ota_UDS_OK;
                        }
                        
                    }
                    //fprintf(USART2_STREAM,"+remain:%d,S-dataLen:%d,i:%d\r\n",lastRemainSize,S19LineData.dataLen,i);

                }
                //记录地址是否连续
                if(lastBinAddr == 0 || lastBinAddr < S19LineData.address){ 
                    lastBinAddr = S19LineData.address;
                }
            }else{
                printf("check s19 error:%d\r\n",17);
                //fprintf(USART2_STREAM,"--data:%s\r\n",readBuff);
                return Ota_UDS_ERR;
            }
            if(fileHead->readPtr >= fileHead->writePtr){
                return Ota_UDS_OK;
            }
        }
        // break;
    case OTA_FILE_HEX:
        for(;;){
            memset(readBuff,0x00,sizeof(readBuff));
            readedLen = ReadFromTextOneLine(fileHead,readBuff,sizeof(readBuff));
            if(readedLen == 0 || readedLen == 0xffff){
                return Ota_UDS_OK;
            }
            ret = (S19ErrCode)validateHEXString((const char*)readBuff,readedLen,&HEXLineData);
            if(HEX_NO_ERROR == ret){
                if(HEX_ELA == HEXLineData.Type){
                    // hexBaseAddr = HEXLineData.baseAddr<<16;
                    // hexLastAddr = hexBaseAddr;
                    //fprintf(USART2_STREAM,"--get new base addr:%x\r\n",hexBaseAddr);
                }
                if(HEX_DATA == HEXLineData.Type){

                    for ( i = 0; i < HEXLineData.dataLen; i++)
                    {
                        dstBuf[(*gotBinSize)] = HEXLineData.data[i];
                        (*gotBinSize)++;
                        if(*gotBinSize == needReadBinLen){
                            //传输完成
                            memset(remainBuf,0x00,sizeof(remainBuf));
                            lastRemainSize = HEXLineData.dataLen - (i+1);
                            if(lastRemainSize) memmove(remainBuf,&HEXLineData.data[i+1],lastRemainSize);
                            //fprintf(USART2_STREAM,"-remain:%d,S-dataLen:%d,i:%d\r\n",lastRemainSize,HEXLineData.dataLen,i);
                            //fprintf(USART2_STREAM,"-[%x][%x][%x][%x][%x][%x]\r\n",remainBuf[0],remainBuf[1],remainBuf[2],remainBuf[3],remainBuf[4],remainBuf[5],remainBuf[6]);
                            return Ota_UDS_OK;
                        }
                    }
                    // if(hexLastAddr == hexBaseAddr){
                    //     hexLastAddr -= HEXLineData.dataLen;
                    // }
                    // if((hexLastAddr + HEXLineData.dataLen) != (hexBaseAddr + HEXLineData.address)){
                    //     //文件结束,已经是下一个文件段了
                    //     fprintf(USART2_STREAM,"--last[%04x] != now[%04x]\r\n",(hexLastAddr),(hexBaseAddr + HEXLineData.address));
                    //     hexLastAddr = hexBaseAddr + HEXLineData.address;
                    //     return Ota_UDS_OK;
                    // }else{
                    //     hexLastAddr = hexBaseAddr + HEXLineData.address;
                    // }
                }
            }else{
                printf("hex error:%d\r\n",12);
                //fprintf(USART2_STREAM,"--data:%s\r\n",readBuff);
                return Ota_UDS_ERR;
            }
            if(fileHead->readPtr >= fileHead->writePtr){
                // hexLastAddr = 0;
                return Ota_UDS_OK;
            }
        }
        // break;       
    default:
        printf("unknow type err:%d\r\n",17);
        break;
    }
    return Ota_UDS_OK;
}


#ifdef PARTFROM_AC7
/**
 * @brief ota for bms solo pack
 * 
 */

void Ota_UDSUpgrade_GANFENGBMSSquence_MainLoop(void){
    extern Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[];
    Diag_SquenceFuncWorkingStateType squenceRet;
    static uint8_t BMSPack = 0;
    static APP_TaskStepType funcStep=THIRD_STEP0;
    static uint8_t chackTimes=0;
    static bool UDSOtaState = 0;
    Ota_UDSErrType readRet;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    uint16_t gotBinSize = 0;
    const uint16_t needReadLen = 0x80;
    static uint32_t gotBinSizeSum = 0;
    // uint16_t oneTimesSendSize = 0x800;
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;
    // const uint8_t RoutineCtrlData[4] = {0x75,0x07,0x86,0xa5};

    // static uint8_t fileIndex = 0;
    static tFileHead fileHead;

    tTaskItem *thisTask;
    thisTask = FindRunnningItem();
    switch (funcStep)
    {
    case THIRD_STEP0:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[GANFENG_START]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            printf("squence do err:%d\r\n",16);
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP1:
        //fprintf(USART2_STREAM,"-uds shakhend compeleted\r\n");
        for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
            if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                break;
            }
        }
        funcStep = THIRD_STEP2;
    break;
    case THIRD_STEP2:
        //请求下载 0x34
        if(OtaStateManage.step_ecu_files > OTA_OTA_FILE+1){
            printf("uds ota false,no file count:%d\r\n",30);
            funcStep = THIRD_STEP_ERROR;
            return;
        }
        targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize; 
        //打开文件
        // fileIndex = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex; //查找文件
        fileHead = OpenFile(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex);
        SetFileOffset(&fileHead,0,0);
        //fprintf(USART2_STREAM,"-fileHead.readPtr:%x\r\n",fileHead.readPtr);
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,Diag_SquenceUDSListCfg[GANFENG_START]->phyId,Diag_SquenceUDSListCfg[GANFENG_START]->responseId,0x00,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP3;
        }
    break;
    case THIRD_STEP3:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            chackTimes = 0;
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OtaStateManage.step_ecu_files);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType);
            gotBinSize = 0;
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
            //fprintf(USART2_STREAM,"-readPtr:%d-Sum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
            gotBinSizeSum += gotBinSize;
            //fprintf(USART2_STREAM,"-gotSum:%d,gotSize:%d\r\n",gotBinSizeSum,gotBinSize);

            if(readRet == Ota_UDS_ERR){
                printf("readRet=-1\r\n");
                //读取失败或者结束
                chackTimes = 0;
                funcStep = THIRD_STEP4;  
                UDSOtaState = FALSE;
            }

            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,Diag_SquenceUDSListCfg[GANFENG_START]->phyId,Diag_SquenceUDSListCfg[GANFENG_START]->responseId,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }
            blockSequenceCounter++;
            if(gotBinSizeSum == OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize){
                //文件发送完备
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] == hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = TRUE;
            }else if(gotBinSizeSum > OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize){
                //接收异常
                //funcStep = THIRD_STEP_ERROR;
                printf("gotBin>hasBin\r\n");
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] > hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }else if (fileHead.readPtr == OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum)
            {
                //TODO:升级失败，文件读完了但是获取的二进制文件却不对应
                printf("file end gotBin!=hasBin\r\n");
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] != hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
        }else{
            if(chackTimes>252){
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
            chackTimes++;
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP4:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,Diag_SquenceUDSListCfg[GANFENG_START]->phyId,Diag_SquenceUDSListCfg[GANFENG_START]->responseId)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    printf("uds ota false,file error\r\n");
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP5;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("negError\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            //错误回应
            printf("re error\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else{
            
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS4\r\n");
    break;
    case THIRD_STEP5:
        //0x31
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            OtaStateManage.step_ecu_files++;
            for(;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;++OtaStateManage.step_ecu_files){
                if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                    break;
                }
            }
            if(OtaStateManage.step_ecu_files > OTA_OTA_FILE){
                //执行结束流程,准备开启第二个服务
                //fprintf(USART2_STREAM,"-uds completed\r\n");
                funcStep = THIRD_STEP8;
            }else{
                if(Diag_OK == Ota_UDSSendRoutineCtrl(&UDSClient,Diag_SquenceUDSListCfg[GANFENG_START]->phyId,Diag_SquenceUDSListCfg[GANFENG_START]->responseId,Diag_StartRoutine,0xff01,NULL,0)){
                    chackTimes = 0;
                    funcStep = THIRD_STEP6;
                }
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            printf("negError\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS5\r\n");
    break;
    case THIRD_STEP6:
        //0x31
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRoutineCtrl(&UDSClient,Diag_SquenceUDSListCfg[GANFENG_START]->phyId,Diag_SquenceUDSListCfg[GANFENG_START]->responseId,Diag_StartRoutine,0xff00,NULL,0)){
                chackTimes = 0;
                funcStep = THIRD_STEP7;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            printf("negError\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS6\r\n");
    break;
    case THIRD_STEP7:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            funcStep = THIRD_STEP2;
            gotBinSizeSum = 0;
            chackTimes=0;
            UDSOtaState = 0;
            blockSequenceCounter = 0;
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            printf("negError\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS7\r\n");
    break;
    case THIRD_STEP8:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[GANFENG_END]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            //如果为双包电池则需要更改ID重新来一遍升级
            if(AC7_2101001C == OtaStateManage.deviceId && 0 == BMSPack){
                //fprintf(USART2_STREAM,"--BMS pack1 ok next one\r\n");
                chackTimes=0;
                UDSOtaState = 0;
                blockSequenceCounter = 0;
                // fileIndex = 0;
                gotBinSizeSum = 0;
                Diag_SquenceUDSListCfg[GANFENG_START]->phyId = 0x111;
                Diag_SquenceUDSListCfg[GANFENG_START]->responseId = 0x611;
                Diag_SquenceUDSListCfg[GANFENG_END]->phyId = 0x111;
                Diag_SquenceUDSListCfg[GANFENG_END]->responseId = 0x611;
                Diag_SquenceUDSListCfg[GANFENG_START]->currentItem = 0;
                Diag_SquenceUDSListCfg[GANFENG_END]->currentItem = 0;
                Diag_SquenceUDSListCfg[GANFENG_START]->ret = Diag_OK;
                Diag_SquenceUDSListCfg[GANFENG_END]->ret = Diag_OK;
                BMSPack ++;
                funcStep = THIRD_STEP0;
            }else{
                funcStep = THIRD_STEP9;
            }
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP9:
        //fprintf(USART2_STREAM,"-uds ota compelete\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    case THIRD_STEP_ERROR:
        //因为某些原因升级任务异常错误中断
        BMSPack = 0;
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        gotBinSizeSum = 0;
        // fileIndex = 0;
        printf("upgrade error\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,"udsErr",strlen("udsErr"));
        TaskToClosed(thisTask);
    break;
    default:
        funcStep = THIRD_STEP0;
    break;
    }
}
void Ota_UDSUpgrade_YingJiLieMCUSquence_MainLoop(void){
    extern Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[];
    Diag_SquenceFuncWorkingStateType squenceRet;
    static APP_TaskStepType funcStep=THIRD_STEP0;
    static uint8_t chackTimes = 0;
    static uint8_t resendTimes = 3; //重发次数
    static bool UDSOtaState = 0;
    Ota_UDSErrType readRet;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    uint16_t gotBinSize = 0;
    uint16_t needReadLen = 0;
    uint16_t oneTimesSendSize = 0x80;
    static uint32_t gotBinSizeSum = 0;
    static uint16_t readTimes = 0,readEndSize = 0; //根据总长度读取次数和余数
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;
    // const uint8_t RoutineCtrlData[4] = {0x75,0x07,0x86,0xa5};
    static uint8_t firmwareStep = 1;
    // static uint8_t fileIndex = 0;
    static tFileHead fileHead;

    tTaskItem *thisTask;
    thisTask = FindRunnningItem();
    switch (funcStep)
    {
    case THIRD_STEP0:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[YINGJIELI_START]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            printf("squence do err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP1:
        //fprintf(USART2_STREAM,"-uds shakhend compeleted\r\n");
        if(OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum == 0){
            //TODO:没有目标文件，升级失败
            printf("uds no file count\r\n");
            TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
            TaskToClosed(thisTask);
        }else{
            firmwareStep = 1;
            funcStep = THIRD_STEP2;
            //打开文件
            // fileIndex = OtaStateManage.downloadFileState[OTA_OTA_FILE].fileIndex; //查找文件
            fileHead = OpenFile(OtaStateManage.downloadFileState[OTA_OTA_FILE].fileIndex);
            SetFileOffset(&fileHead,0,0);
        }
    break;
    case THIRD_STEP2:
        //请求下载 0x34
        targetFirmwareAddr = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].BinHeadAddr;
        targetFirmwareLen = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize; 

        readTimes = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize / oneTimesSendSize;
        readEndSize = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize % oneTimesSendSize;

        gotBinSizeSum = 0;
        gotBinSize = 0;
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,0x11,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP3;
        }
    break;
    case THIRD_STEP3:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            chackTimes = 0;
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OTA_OTA_FILE);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OTA_OTA_FILE].fileType);
	        gotBinSize = 0;
            readTimes?(needReadLen = oneTimesSendSize):(needReadLen = readEndSize);
            if(readTimes) readTimes --;
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OTA_OTA_FILE].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
	        //fprintf(USART2_STREAM,"-gotBinSizeSum:%d,gotBinSize:%d\r\n",gotBinSizeSum,gotBinSize);
            //fprintf(USART2_STREAM,"-fileHead.readPtr:%d-downloadEcuFileSizeSum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum);
	        gotBinSizeSum += gotBinSize;
            if(readRet == Ota_UDS_ERR){
                printf("readRet = -1\r\n");
                //读取失败或者结束
                funcStep = THIRD_STEP4;  
                UDSOtaState = FALSE;
            }
            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }
            blockSequenceCounter++;
            if(gotBinSizeSum == OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize){
                //文件发送完备
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] == hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize);
                funcStep = THIRD_STEP4;
                UDSOtaState = TRUE;
            }else if(gotBinSizeSum > OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize){
                //接收异常
                printf("gotBin>hasBin\r\n");
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] > hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize);
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }else if (fileHead.readPtr == OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum)
            {
                //升级失败，文件读完了但是获取的二进制文件却不对应
                printf("gotBin!=hasBin\r\n");
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] != hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[firmwareStep].hasBinSize);
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("neg responce error\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else{
            //TODO:发送超时
            if(chackTimes>252){
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
                chackTimes = 0;
            }
            chackTimes++;
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP4:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    printf("file error\r\n");
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP5;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("neg responce err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            //错误回应
            printf("responce err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else{

        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS4\r\n");
    break;
    case THIRD_STEP5:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            firmwareStep++;
            if(firmwareStep > 2){
                //执行结束流程,准备开启第二个服务
                //fprintf(USART2_STREAM,"-uds firmware transmit completed\r\n");
                funcStep = THIRD_STEP7;
            }else{
                funcStep = THIRD_STEP6;
                resendTimes = 3;
            }
            chackTimes = 0;
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            printf("neg err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))printf("timeOutS5\r\n");
    break;
    case THIRD_STEP6:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[YINGJIELI_CONTINUE]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            gotBinSizeSum = 0;
            funcStep = THIRD_STEP2;
            blockSequenceCounter = 0;
            UDSOtaState = 0;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            if (resendTimes)
            {
                printf("squence resend\r\n");
                Diag_UDSSquenceListDefaultReset(Diag_SquenceUDSListCfg[YINGJIELI_CONTINUE],0);
            }else{
                printf("squence error\r\n");
                funcStep = THIRD_STEP_ERROR;
            }
            resendTimes--;
        }
    break;
    case THIRD_STEP7:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[YINGJIELI_END]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP8;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            printf("squence err\r\n");
            TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
            TaskToClosed(thisTask);
        }
    break;
    case THIRD_STEP8:
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    case THIRD_STEP_ERROR:
        //因为某些原因升级任务异常错误中断
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        gotBinSizeSum = 0;
        firmwareStep = 1;
        // fileIndex = 0;
        printf("upgrade err\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,"udsErr",strlen("udsErr"));
        TaskToClosed(thisTask);
    break;
    default:
        funcStep = THIRD_STEP0;
    break;
    }
}

void Ota_UDSUpgradeFor_AnpeiMCUSquence_MainLoop(void){
    extern Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[];
    Diag_SquenceFuncWorkingStateType squenceRet;
    static APP_TaskStepType funcStep=THIRD_STEP0;
    static uint8_t chackTimes=0;
    static bool UDSOtaState = 0;
    Ota_UDSErrType readRet;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    uint16_t gotBinSize = 0,needReadLen = 0;
    uint16_t oneTimesSendSize = 0x800;
    static uint16_t readTimes = 0,readEndSize = 0; //根据总长度读取次数和余数
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;

    // static uint8_t fileIndex = 0;
    static tFileHead fileHead;

    tTaskItem *thisTask;
    thisTask = FindRunnningItem();
    switch (funcStep)
    {
    case THIRD_STEP0:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[ANPEI_START]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            printf("squence err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP1:
        //fprintf(USART2_STREAM,"-uds shakhend compeleted\r\n");
        for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
            if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                break;
            }
        }
        funcStep = THIRD_STEP2;
    break;
    case THIRD_STEP2:
        //请求下载 0x34
        if(OtaStateManage.step_ecu_files > OTA_OTA_FILE+1){
            //TODO:升级结束，因为没有需要升级的文件内容
            printf("no file count\r\n");
            TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
            TaskToClosed(thisTask);
            return;
        }
        //次为安沛的MCU诊断升级任务，所以针对安沛的进行开发，其他的MCU则以下面的方式进行复刻
        targetFirmwareAddr = 0x08008000;//OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize; 
        //打开文件
        // fileIndex = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex; //查找文件
        fileHead = OpenFile(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex);
        SetFileOffset(&fileHead,0,0);
        readTimes = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum / oneTimesSendSize;
        readEndSize = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum % oneTimesSendSize;
        //fprintf(USART2_STREAM,"-readTimes:%d,readEndSize:%d \r\n",readTimes,readEndSize);
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,01,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP3;
        }
    break;
    case THIRD_STEP3:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            chackTimes = 0;
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OtaStateManage.step_ecu_files);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType);
            readTimes?(needReadLen = oneTimesSendSize):(needReadLen = readEndSize);
            if(readTimes) readTimes --;
            //fprintf(USART2_STREAM,"-readTimes:%d,needReadLen:%d\r\n",readTimes,needReadLen);
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
            //fprintf(USART2_STREAM,"-fileHead.readPtr:%d-downloadEcuFileSizeSum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
            if(readRet == Ota_UDS_ERR){
                printf("readRet = -1\r\n");
                //读取失败或者结束
                funcStep = THIRD_STEP4;  
                UDSClient.RecvDataType.responseState = Diag_RESPONSE_POS;
                UDSOtaState = FALSE;
            }

            if(fileHead.readPtr == OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum){
                //文件发送完备
                //fprintf(USART2_STREAM,"-readPtr[%d] == downloadEcuFileSizeSum[%d]\r\n",fileHead.readPtr, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
                funcStep = THIRD_STEP4;
                UDSClient.RecvDataType.responseState = Diag_RESPONSE_POS;
                UDSOtaState = TRUE;
            }else if(fileHead.readPtr > OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum){
                //接收异常
                //funcStep = THIRD_STEP_ERROR;
                printf("readPtr>down\r\n");
                //fprintf(USART2_STREAM,"-readPtr[%d] > downloadEcuFileSizeSum[%d]\r\n",fileHead.readPtr, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
                UDSClient.RecvDataType.responseState = Diag_RESPONSE_POS;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }
            blockSequenceCounter++;
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("neg err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            printf("res err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP4:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    printf("file error\r\n");
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP5;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("neg err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            //错误回应
            printf("res err\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else{
            
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            printf("timeOutInS4\r\n");
    break;
    case THIRD_STEP5:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[ANPEI_END]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP6;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            printf("squence err\r\n");
            TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
            TaskToClosed(thisTask);
        }
    break;
    case THIRD_STEP6:
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        readTimes = 0,readEndSize = 0; //根据总长度读取次数和余数
        // fileIndex = 0;
        //fprintf(USART2_STREAM,"-uds ota compelete\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    case THIRD_STEP_ERROR:
        //因为某些原因升级任务异常错误中断
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        readTimes = 0,readEndSize = 0; //根据总长度读取次数和余数
        // fileIndex = 0;
        printf("upgrade error\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,"udsErr",strlen("udsErr"));
        TaskToClosed(thisTask);
    break;
    default:
        funcStep = THIRD_STEP0;
    break;
    }
}

#endif


#ifdef ANPEI_MCU_BY_STEP

void Ota_UDSUpgradeFor_AnpeiMCU_MainLoop(void){
    uint8_t ret = 0xff;
    Ota_UDSErrType readRet;
    static uint8_t funcStep=THIRD_STEP0;
    static uint8_t chackTimes=0;
    const uint8_t RoutineCtrlData[9] = {0x44,0x08,0x00,0x80,0x00,0x00,0x01,0x1D,0x30};
    const uint8_t RoutineCtrlEndData[4] = {0x96,0x73,0xbf,0x44};
    tTaskItem *thisTask;
    thisTask = FindRunnningItem();
    static uint8_t fileIndex = 0;
    static tFileHead fileHead;
    uint16_t gotBinSize = 0,needReadLen = 0;
    uint16_t oneTimesSendSize = 0x800;
    static uint16_t readTimes = 0,readEndSize = 0; //根据总长度读取次数和余数
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    static bool UDSOtaState = 0;
    static uint32_t fileSize = 0;
    static uint32_t UDSSeed = 0;
    uint32_t UDSKey = 0;
    //fprintf(USART2_STREAM,".");
    fprintf(USART2_STREAM,"-step:%d\r\n",funcStep);
    switch (funcStep)
    {
    case THIRD_STEP0:
        if(Diag_OK == Ota_UDSInSession(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,Diag_ExtendedDiagnostic)){
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP1:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            //请求种子
            if(Diag_OK == Ota_UDSSendSecurityAccess(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,0x01,NULL,0)){
                chackTimes = 0;
                funcStep = THIRD_STEP2;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP2:
        //计算和发送种子
        UDSSeed = (UDSSeed = ((UDSSeed = UDSClient.recvBuf[3])<<8 | UDSClient.recvBuf[4]) << 8 | UDSClient.recvBuf[5]) << 8 | UDSClient.recvBuf[6];
        fprintf(USART2_STREAM,"get seed:%x",UDSSeed);
        UDSKey = Diag_KeyComputation(UDSSeed,UDS_MCU,35);
        fprintf(USART2_STREAM,"get key:%x",UDSKey);
        sendBuf[0] = (UDSKey >> 24) & 0xff;
        sendBuf[1] = (UDSKey >> 16) & 0xff;
        sendBuf[2] = (UDSKey >> 8) & 0xff;
        sendBuf[3] = (UDSKey >> 0) & 0xff;
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendSecurityAccess(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,0x02,sendBuf,4)){
                chackTimes = 0;
                funcStep = THIRD_STEP3;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;  
    case THIRD_STEP3:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            fprintf(USART2_STREAM,"-seed send pass--\r\n");
            if(Diag_OK == Ota_UDSInSession(&UDSClient,UDS_RQID_FUN,UDS_RPID_MCU,Diag_DefaultSession)){
                chackTimes = 0;
                funcStep = THIRD_STEP4;
            }
        }else{
            fprintf(USART2_STREAM,"-seed send unpass--\r\n");
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP4:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            fprintf(USART2_STREAM,"-uds postive--\r\n");
            if(Diag_OK == Ota_UDSInSession(&UDSClient,UDS_RQID_FUN,UDS_RPID_MCU,Diag_ExtendedDiagnostic)){
                chackTimes = 0;
                funcStep = THIRD_STEP5;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            fprintf(USART2_STREAM,"-uds negetive--\r\n");
            //消极响应,继续发送，如果失败则失败
        }else if(Diag_RESPONSE_RUNNING == UDSClient.RecvDataType.responseState){
            fprintf(USART2_STREAM,"-running--\r\n");
        }else{
            //TODO:发送超时
            fprintf(USART2_STREAM,"-uds time out--\r\n");
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP5:    
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRoutineCtrl(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,Diag_StartRoutine,0x0203,NULL,0x00)){
                chackTimes = 0;
                funcStep = THIRD_STEP6;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP6:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSDTCCtrl(&UDSClient,UDS_RQID_FUN,UDS_RPID_MCU,Diag_DtcOff)){
                chackTimes = 0;
                funcStep = THIRD_STEP7;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP7:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendCommCtrl(&UDSClient,UDS_RQID_FUN,UDS_RPID_MCU,Diag_DisableRxAndTx,Diag_NormalCommunicationMessages)){
                chackTimes = 0;
                funcStep = THIRD_STEP8;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP8:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            //进入编程会话
            if(Diag_OK == Ota_UDSInSession(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,Diag_ProgrammingSession)){
                chackTimes = 0;
                funcStep = THIRD_STEP9;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP9:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            //请求种子
            if(Diag_OK == Ota_UDSSendSecurityAccess(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,0x11,NULL,0)){
                chackTimes = 0;
                funcStep = THIRD_STEP10;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP10:
        //计算和发送种子
        UDSSeed = (UDSSeed = ((UDSSeed = UDSClient.recvBuf[3])<<8 | UDSClient.recvBuf[4]) << 8 | UDSClient.recvBuf[5]) << 8 | UDSClient.recvBuf[6];
        fprintf(USART2_STREAM,"get seed:%x",UDSSeed);
        UDSKey = Diag_KeyComputation(UDSSeed,UDS_MCU,35);
        fprintf(USART2_STREAM,"get key:%x",UDSKey);
        sendBuf[0] = (UDSKey >> 24) & 0xff;
        sendBuf[1] = (UDSKey >> 16) & 0xff;
        sendBuf[2] = (UDSKey >> 8) & 0xff;
        sendBuf[3] = (UDSKey >> 0) & 0xff;
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendSecurityAccess(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,0x12,sendBuf,4)){
                chackTimes = 0;
                funcStep = THIRD_STEP11;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP11:
        //0x31:例程控制- 
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRoutineCtrl(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,Diag_StartRoutine,0xff00,RoutineCtrlData,sizeof(RoutineCtrlData))){
                chackTimes = 0;
                funcStep = THIRD_STEP12;  
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP12:
        fprintf(USART2_STREAM,"-uds responseState:%d--\r\n",UDSClient.RecvDataType.responseState);
        
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
                if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                    break;
                }
            }
            funcStep = THIRD_STEP13;
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            funcStep = THIRD_STEP11;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            funcStep = THIRD_STEP11;
        }else{

        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP13:
        //请求下载 0x34
        if(OtaStateManage.step_ecu_files > OTA_OTA_FILE+1){
            //TODO:升级结束，因为没有需要升级的文件内容
            fprintf(USART2_STREAM,"-uds ota false,becouse no file count\r\n");
            TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
            TaskToClosed(thisTask);
            return;
        }
        //TODO:需要增加不同升级文件的传输选择，包含地址，大小等，一次升级有三个文件需要传输
        //次为安沛的MCU诊断升级任务，所以针对安沛的进行开发，其他的MCU则以下面的方式进行复刻
        targetFirmwareAddr = 0x08008000;//OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize; 
        // if(OtaStateManage.OtaEcuData.fileData->fileType == OTA_FILE_BIN){
        //     targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        //     targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize;            
        // }else if(OtaStateManage.OtaEcuData.fileData->fileType == OTA_FILE_S19){
        //     targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        //     targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize;
        // }else if(OtaStateManage.OtaEcuData.fileData->fileType == OTA_FILE_HEX){
        //     // if(!strcmp(OtaStateManage.OtaEcuData.OtaParam.deviceId,"AC7-2104010")){
        //     //     //英吉利的MCU两个文件放在了一个HEX文件里，按照这种方式进行读取
        //     //     targetFirmwareAddr = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[1].BinHeadAddr;
        //     //     targetFirmwareLen = OtaStateManage.downloadFileState[OTA_OTA_FILE].hexBinMsg[1].hasBinSize;
        //     // }
        // }else{
        //     //TODO:为找到匹配文件
        // }
        //打开文件
        fileIndex = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex; //查找文件
        fileHead = OpenFile(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex);
        readTimes = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum / oneTimesSendSize;
        readEndSize = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum % oneTimesSendSize;
        fprintf(USART2_STREAM,"-readTimes:%d,readEndSize:%d \r\n",readTimes,readEndSize);
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,01,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP14;
        }
    break;
    case THIRD_STEP14:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OtaStateManage.step_ecu_files);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType);
            readTimes?(needReadLen = oneTimesSendSize):(needReadLen = readEndSize);
            if(readTimes) readTimes --;
            fprintf(USART2_STREAM,"-readTimes:%d,needReadLen:%d\r\n",readTimes,needReadLen);
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
            fprintf(USART2_STREAM,"-fileHead.readPtr:%d-downloadEcuFileSizeSum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);

            // fprintf(USART2_STREAM,"-get bin data len:%d-data:\r\n",gotBinSize);
            // for (size_t i = 0; i < gotBinSize; i++)
            // {
            //     fprintf(USART2_STREAM,"%02x",sendBuf[i]);
            // }
            // fprintf(USART2_STREAM,"\r\n");

            if(readRet == Ota_UDS_ERR){
                fprintf(USART2_STREAM,"-readRet = -1 go step 15\r\n");
                //读取失败或者结束
                funcStep = THIRD_STEP15;  
                UDSOtaState = FALSE;
            }

            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }
            if(blockSequenceCounter >= 0xff) blockSequenceCounter = 0;
            blockSequenceCounter++;
            if(fileHead.readPtr == OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum){
                //文件发送完备
                fprintf(USART2_STREAM,"-readPtr[%d] == downloadEcuFileSizeSum[%d]\r\n",fileHead.readPtr, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
                funcStep = THIRD_STEP15;
                UDSOtaState = TRUE;
            }else if(fileHead.readPtr > OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum){
                //接收异常
                //funcStep = THIRD_STEP_ERROR;
                fprintf(USART2_STREAM,"-readPtr[%d] > downloadEcuFileSizeSum[%d]\r\n",fileHead.readPtr, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);

                funcStep = THIRD_STEP15;
                UDSOtaState = FALSE;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            UDSOtaState = FALSE;
        }else{
            //TODO:发送超时
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP15:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    fprintf(USART2_STREAM,"-uds ota false,becouse file error\r\n");
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP16;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP16:
        //0x31
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            OtaStateManage.step_ecu_files++;
            for(;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;++OtaStateManage.step_ecu_files){
                if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                    break;
                }
            }
            if(OtaStateManage.step_ecu_files > OTA_OTA_FILE){
                fprintf(USART2_STREAM,"-uds firmware transmit completed\r\n");
                if(Diag_OK == Ota_UDSSendRoutineCtrl(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,Diag_StartRoutine,0x0202,RoutineCtrlEndData,sizeof(RoutineCtrlEndData))){
                    chackTimes = 0;
                    funcStep = THIRD_STEP17;
                }
            }else{
                funcStep = THIRD_STEP13;
            }
        }else{
            //消极响应或错误,继续发送，如果失败则失败
            CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
        }
    break;
    case THIRD_STEP17:
        //升级结束，准备重启
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendEcuReset(&UDSClient,UDS_RQID_MCU,UDS_RPID_MCU,01)){
                chackTimes = 0;
                funcStep = THIRD_STEP18;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
        }else{
            //TODO:发送超时
        }
        CheckRequestTimesOutAndClosed(thisTask,&chackTimes,10,&funcStep);
    break;
    case THIRD_STEP18:
        fprintf(USART2_STREAM,"-uds ota compelete\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    }
}


void OTA_UDSTest(){

	fprintf(USART2_STREAM,"-uds test begin--\r\n");
	if (0x01 == ThirdHasBeenCreat(Ota_UDSUpgradeFor_AnpeiMCU_MainLoop))
		CreatThird("OtaM",To_OS_Slice_Time(100,OS_MSEC),10,Ota_UDSUpgradeFor_AnpeiMCU_MainLoop);
}
#endif




//-----------------------------------------------------AC08--------------------------------------------
#ifdef PARTFROM_AC8 

//-----------------------------------------OBC-------------------------------------------------------
void Ota_UDSUpgrade_AC08OBC_MainLoop(void){
    extern Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[];
    Diag_SquenceFuncWorkingStateType squenceRet;
    static uint8_t BMSPack = 0;
    static APP_TaskStepType funcStep=THIRD_STEP0;
    static uint8_t chackTimes=0;
    static bool UDSOtaState = 0;
    Ota_UDSErrType readRet;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    uint16_t gotBinSize = 0;
    const uint16_t needReadLen = 0x400;
    static uint32_t gotBinSizeSum = 0;
    uint16_t oneTimesSendSize = 0x800;
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;
    const uint8_t RoutineCtrlData[4] = {0x75,0x07,0x86,0xa5};

    static uint8_t fileIndex = 0;
    static tFileHead fileHead;
    static uint8_t crcflag = 0;
    static uint32_t crcnum = 0;
    static uint32_t crcdata;


    tTaskItem *thisTask;
    thisTask = FindRunnningItem();
    switch (funcStep)
    {
    case THIRD_STEP0:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[AC08_OBC_START]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            USART_Send(USART2_SFR,"squence do err\r\n",16);
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP1:
        //fprintf(USART2_STREAM,"-uds shakhend compeleted\r\n");
        for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
            if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                break;
            }
        }
        funcStep = THIRD_STEP2;
    break;
    case THIRD_STEP2:
        //请求下载 0x34
        if(OtaStateManage.step_ecu_files > OTA_OTA_FILE+1){
            USART_Send(USART2_SFR,"uds ota false,no file count\r\n",30);
            funcStep = THIRD_STEP_ERROR;
            return;
        }
        targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize;


        //打开文件
        fileIndex = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex; //查找文件
        fileHead = OpenFile(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex);
        SetFileOffset(&fileHead,0,0);
        fprintf(USART2_STREAM,"-fileHead.readPtr:%x\r\n",fileHead.readPtr);
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,UDS_RQID_OBC,UDS_RPID_OBC,0x00,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP3;
        }
    break;
    case THIRD_STEP3:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            chackTimes = 0;
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OtaStateManage.step_ecu_files);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType);
            gotBinSize = 0;
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
            //fprintf(USART2_STREAM,"-readPtr:%d-Sum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
            gotBinSizeSum += gotBinSize;
            //fprintf(USART2_STREAM,"-gotSum:%d,gotSize:%d\r\n",gotBinSizeSum,gotBinSize);
            if(crcflag == 0)
            {
                crcdata = 0;
                crcdata = 0xFFFFFFFF;
                crcflag++;
            }else{
                
                for (;crcnum< gotBinSize; crcnum++)
                {
                    
                    crcdata = crc32_table[(crcdata ^ sendBuf[crcnum]) & 0xFF] ^ ((crcdata >> 8) & 0x00FFFFFF);
                }
                crcnum =0;
            }
            if(readRet == Ota_UDS_ERR){
                USART_Send(USART2_SFR,"readRet=-1\r\n",12);
                //读取失败或者结束
                chackTimes = 0;
                funcStep = THIRD_STEP4;  
                UDSOtaState = FALSE;
            }

            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,UDS_RQID_OBC,UDS_RPID_OBC,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }
            blockSequenceCounter++;
            if(gotBinSizeSum == OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize){
                crcdata = crcdata ^ 0xFFFFFFFF; 
                crcflag = 0;
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = TRUE;
                fprintf(USART2_STREAM,"crcdata:%x\r\n",crcdata);
            }else if(gotBinSizeSum > OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize){
                //接收异常
                //funcStep = THIRD_STEP_ERROR;
                USART_Send(USART2_SFR,"gotBin>hasBin\r\n",15);
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] > hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }else if (fileHead.readPtr == OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum)
            {
                //TODO:升级失败，文件读完了但是获取的二进制文件却不对应
                USART_Send(USART2_SFR,"file end gotBin!=hasBin\r\n",25);
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] != hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
        }else{
            if(chackTimes>252){
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
            chackTimes++;
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP4:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,Diag_SquenceUDSListCfg[AC08_OBC_START]->phyId,Diag_SquenceUDSListCfg[AC08_OBC_START]->responseId)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    USART_Send(USART2_SFR,"uds ota false,file error\r\n",26);
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP7;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            USART_Send(USART2_SFR,"negError\r\n",10);
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            //错误回应
            USART_Send(USART2_SFR,"re error\r\n",10);
            funcStep = THIRD_STEP_ERROR;
        }else{
            
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            USART_Send(USART2_SFR,"timeOutInS4\r\n",13);
    break;

    case THIRD_STEP5:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            OtaStateManage.step_ecu_files++;
            for(;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;++OtaStateManage.step_ecu_files){
                if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                    break;
                }
            }
            if(OtaStateManage.step_ecu_files > OTA_OTA_FILE){
                //执行结束流程,准备开启第二个服务
                fprintf(USART2_STREAM,"-uds completed\r\n");
                funcStep = THIRD_STEP7;
            }else{
                funcStep = THIRD_STEP6;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            USART_Send(USART2_SFR,"negError\r\n",10);
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            USART_Send(USART2_SFR,"timeOutInS5\r\n",13);
    break;
    case THIRD_STEP6:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[AC08_BMS_CONTINUE]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            gotBinSizeSum = 0;
            funcStep = THIRD_STEP2;
            blockSequenceCounter = 0;
            UDSOtaState = 0;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            USART_Send(USART2_SFR,"squence error\r\n",15);
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP7:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[AC08_OBC_END]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes=0;
            funcStep = THIRD_STEP8;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP8:
        fprintf(USART2_STREAM,"-uds ota compelete\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    case THIRD_STEP_ERROR:
        //因为某些原因升级任务异常错误中断
        BMSPack = 0;
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        gotBinSizeSum = 0;
        fileIndex = 0;
        USART_Send(USART2_SFR,"upgrade error\r\n",15);
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,"udsErr",strlen("udsErr"));
        TaskToClosed(thisTask);
    break;
    default:
        funcStep = THIRD_STEP0;
    break;
    }
}

void Ota_UDSUpgrade_AC08_MainLoop(void){
    extern Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[];
    Diag_SquenceFuncWorkingStateType squenceRet;
    // static uint8_t BMSPack = 0;
    static APP_TaskStepType funcStep=THIRD_STEP0;
    static uint8_t chackTimes=0;
    static bool UDSOtaState = 0;
    Ota_UDSErrType readRet;
    static uint8_t blockSequenceCounter = 0;
    uint32_t targetFirmwareAddr = 0;
    uint32_t targetFirmwareLen = 0;
    uint16_t gotBinSize = 0;
    const uint16_t needReadLen = 0x80;
    static uint32_t gotBinSizeSum = 0;
    // uint16_t oneTimesSendSize = 0x800;
    uint8_t sendBuf[2048] = {0};
    extern t_TigerTallyLock TigerTallyLockEcu;
    // const uint8_t RoutineCtrlData[4] = {0x75,0x07,0x86,0xa5};
    static uint8_t processstart=0;
    static uint8_t processcontinue=0;
    static uint8_t processend=0;
    static uint8_t ecuid;
    ecuid = OtaStateManage.ecuType;
    // static uint8_t fileIndex = 0;
    static tFileHead fileHead;
    printf("Ota_UDSUpgrade_AC08_MainLoop-funcStep:%d\r\n", funcStep);
    //fprintf(USART2_STREAM,"ecutype=%d\r\n",ecuid);

    static uint8_t crcflag = 0;
    static uint32_t crcnum = 0;
    static uint32_t crcdata;

    switch (ecuid)
    {
        case OTA_MCU:
        case OTA_VCU:
            processstart = AC08_VCU_START;
            processcontinue = 0;
            processend = AC08_VCU_END;
            targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].hexBinMsg[1].BinHeadAddr;
            targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].hexBinMsg[1].hasBinSize;
            break;
        case OTA_BMS:
            processstart = AC08_BMS_START;
            processcontinue = AC08_BMS_CONTINUE;
            processend = AC08_BMS_END;
            targetFirmwareAddr = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
            targetFirmwareLen = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize;
            break;
    }
    tTaskItem *thisTask;
    thisTask = FindRunnningItem();

    switch (funcStep)
    {
    case THIRD_STEP0:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[processstart]);
        printf("Ota_UDSUpgrade_AC08_MainLoop-squenceRet:%d\r\n", squenceRet);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            funcStep = THIRD_STEP1;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            USART_Send(USART2_SFR,"squence do err\r\n",16);
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP1:
        //fprintf(USART2_STREAM,"-uds shakhend compeleted\r\n");
        for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
            if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                break;
            }
        }
        funcStep = THIRD_STEP2;
    break;
    case THIRD_STEP2:
        //请求下载 0x34
        if(OtaStateManage.step_ecu_files > OTA_OTA_FILE+1){
            printf("uds ota false,no file count\r\n");
            funcStep = THIRD_STEP_ERROR;
            return;
        }
        
        //鎵撳紑鏂囦欢
        // fileIndex = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex; //鏌ユ壘鏂囦欢
        fileHead = OpenFile(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex);
        SetFileOffset(&fileHead,0,0);
        fprintf(USART2_STREAM,"-fileHead.readPtr:%x\r\n",fileHead.readPtr);
        printf("uds,ac08,mainLoop,thirdStep2, \
        targetFirmwareAddr:0x%x,  \
        targetFirmwareLen:%d, \
        step_ecu_files:%d, \
        fileIndex:%d\r\n", 
            targetFirmwareAddr,
            targetFirmwareLen,
            OtaStateManage.step_ecu_files,
            OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].fileIndex
        );
        //TODO:检查文件有效性
        if(Diag_OK == Ota_UDSSendRequestDownload(&UDSClient,Diag_SquenceUDSListCfg[processstart]->phyId,Diag_SquenceUDSListCfg[processstart]->responseId,0x00,0x44,targetFirmwareAddr,targetFirmwareLen)){
            chackTimes = 0;
            blockSequenceCounter = 1;
            funcStep = THIRD_STEP3;
        }
    break;
    case THIRD_STEP3:
        //开始传输数据 0x36
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            chackTimes = 0;
            //fprintf(USART2_STREAM,"-get file index is:%d\r\n",OtaStateManage.step_ecu_files);
            //fprintf(USART2_STREAM,"-get file type is:%d\r\n",OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType);
            gotBinSize = 0;
            readRet = OTA_PrepareSendFrimData(&fileHead,OtaStateManage.OtaEcuData.fileData[OtaStateManage.step_ecu_files].fileType,needReadLen,sendBuf,sizeof(sendBuf),&gotBinSize);
            //fprintf(USART2_STREAM,"-readPtr:%d-Sum:%d\r\n",fileHead.readPtr,OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum);
            gotBinSizeSum += gotBinSize;
            fprintf(USART2_STREAM,"-gotSum:%d,gotSize:%d\r\n",gotBinSizeSum,gotBinSize);

             if(crcflag == 0)
            {
                crcdata = 0;
                crcdata = 0xFFFFFFFF;
                crcflag++;
            }else{
                
                for (;crcnum< gotBinSize; crcnum++)
                {
                    
                    crcdata = crc32_table[(crcdata ^ sendBuf[crcnum]) & 0xFF] ^ ((crcdata >> 8) & 0x00FFFFFF);
                }
                crcnum =0;
            }

            if(readRet == Ota_UDS_ERR){
                printf("readRet=-1\r\n");
                //读取失败或者结束
                chackTimes = 0;
                funcStep = THIRD_STEP4;  
                UDSOtaState = FALSE;
            }

            if(Diag_OK == Ota_UDSSendTransferData(&UDSClient,Diag_SquenceUDSListCfg[processstart]->phyId,Diag_SquenceUDSListCfg[processstart]->responseId,blockSequenceCounter,0xff,sendBuf,gotBinSize)){
                chackTimes = 0;
            }

            blockSequenceCounter++;
            if(gotBinSizeSum == targetFirmwareLen){
                //文件发送完备
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] == hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                crcdata = crcdata ^ 0xFFFFFFFF; 
                crcflag = 0;
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = TRUE;
                printf("crcdata:0x%x\r\n", crcdata);
            }else if(gotBinSizeSum > targetFirmwareLen){
                //接收异常
                //funcStep = THIRD_STEP_ERROR;
                printf("gotBin>hasBin, gotBinSizeSum:%d, targetFirmwareLen:%d\r\n", gotBinSizeSum,targetFirmwareLen);
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] > hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }else if (fileHead.readPtr == OtaStateManage.downloadFileState[OTA_OTA_FILE].downloadEcuFileSizeSum)
            {
                //TODO:升级失败，文件读完了但是获取的二进制文件却不对应
                printf("file end gotBin!=hasBin\r\n");
                //fprintf(USART2_STREAM,"-gotBinSizeSum[%d] != hasBinSize[%d]\r\n",gotBinSizeSum, OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize);
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
                printf("Ota_UDSUpgrade_AC08_MainLoop-step3-Diag_RESPONSE_NEG...\r\n");
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
        }else{
            if(chackTimes>252){
                printf("Ota_UDSUpgrade_AC08_MainLoop-step3-chackTimes>252...\r\n");
                chackTimes = 0;
                funcStep = THIRD_STEP4;
                UDSOtaState = FALSE;
            }
            chackTimes++;
        }
        //此次不能安置超时检查，因为要重复发送多次
    break;
    case THIRD_STEP4:
        //结束传输
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            if(Diag_OK == Ota_UDSSendRequestTransferExit(&UDSClient,Diag_SquenceUDSListCfg[processstart]->phyId,Diag_SquenceUDSListCfg[processstart]->responseId)){
                chackTimes = 0;
                if(FALSE == UDSOtaState){
                    USART_Send(USART2_SFR,"uds ota false,file error\r\n",26);
                    TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,NULL,0);
                    TaskToClosed(thisTask);
                    return;
                }
                funcStep = THIRD_STEP5;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应,继续发送，如果失败则失败
            printf("negError\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else if(Diag_RESPONSE_ERR == UDSClient.RecvDataType.responseState){
            //错误回应
            printf("re error\r\n");
            funcStep = THIRD_STEP_ERROR;
        }else{
            
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            USART_Send(USART2_SFR,"timeOutInS4\r\n",13);
    break;

    case THIRD_STEP5:
        if(Diag_RESPONSE_POS == UDSClient.RecvDataType.responseState){
            OtaStateManage.step_ecu_files++;
            for(;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;++OtaStateManage.step_ecu_files){
                if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                    break;
                }
            }
            if(OtaStateManage.step_ecu_files > OTA_OTA_FILE){
                //执行结束流程,准备开启第二个服务
                fprintf(USART2_STREAM,"-uds completed\r\n");
                funcStep = THIRD_STEP7;
            }else{
                funcStep = THIRD_STEP6;
            }
        }else if(Diag_RESPONSE_NEG == UDSClient.RecvDataType.responseState){
            //消极响应或错误,继续发送，如果失败则失败
            USART_Send(USART2_SFR,"negError\r\n",10);
            funcStep = THIRD_STEP_ERROR;
        }
        if(CheckRequestTimesOutAndGoToErrStep(&chackTimes,10,&funcStep,THIRD_STEP_ERROR))
            USART_Send(USART2_SFR,"timeOutInS5\r\n",13);
    break;
    case THIRD_STEP6:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[processcontinue]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes = 0;
            gotBinSizeSum = 0;
            funcStep = THIRD_STEP2;
            blockSequenceCounter = 0;
            UDSOtaState = 0;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            USART_Send(USART2_SFR,"squence error\r\n",15);
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP7:
        squenceRet = Diag_UDSSquenceDoing(Diag_SquenceUDSListCfg[processend]);
        if (Diag_SQUENCE_SUCCEED == squenceRet){
            //fprintf(USART2_STREAM,"--squence doing ok\r\n");
            chackTimes=0;
            funcStep = THIRD_STEP8;
        }else if (Diag_SQUENCE_FAIL == squenceRet){
            funcStep = THIRD_STEP_ERROR;
        }
    break;
    case THIRD_STEP8:
        fprintf(USART2_STREAM,"-uds ota compelete\r\n");
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_SUCCESS,NULL,0);
        TaskToClosed(thisTask);
    break;
    case THIRD_STEP_ERROR:
        //因为某些原因升级任务异常错误中断
        // BMSPack = 0;
        funcStep=THIRD_STEP0;
        chackTimes=0;
        UDSOtaState = 0;
        blockSequenceCounter = 0;
        gotBinSizeSum = 0;
        // fileIndex = 0;
        USART_Send(USART2_SFR,"upgrade error\r\n",15);
        TigerTallyLockBack(&TigerTallyLockEcu,TALLY_RETURN_ERROR,"udsErr",strlen("udsErr"));
        TaskToClosed(thisTask);
    break;
    default:
        funcStep = THIRD_STEP0;
    break;
    }
}

#endif
//--------------------------------------------------------------------------------------------------
